<!-- 仪表盘页面 -->
<template>
  <div class="app-container">
    <main class="main-content">
      <div class="stats-container">
        <div class="stat-card">
          <h3>{{ stats.total }}</h3>
          <p>总计划数</p>
        </div>
        <div class="stat-card">
          <h3>{{ stats.inProgress }}</h3>
          <p>进行中</p>
        </div>
        <div class="stat-card">
          <h3>{{ stats.completed }}</h3>
          <p>已完成</p>
        </div>
        <div class="stat-card">
          <h3>{{ stats.pending }}</h3>
          <p>待审核</p>
        </div>
      </div>

      <div class="card">
        <div class="card-header">
          <h3>生产计划列表</h3>
          <button class="btn btn-warning" @click="showPlanModal = true">创建新计划</button>
        </div>
        <div class="card-body">
          <table>
            <thead>
              <tr>
                <th>卡片号</th>
                <th>产品名称</th>
                <th>数量</th>
                <th>状态</th>
                <th>进度</th>
                <th>截止日期</th>
                <th>操作</th>
              </tr>
            </thead>
            <tbody>
              <template v-if="plans.length > 0">
                <tr v-for="plan in plans" :key="plan.id">
                  <td>{{ plan.cardNumber }}</td>
                  <td>{{ plan.product }}</td>
                  <td>{{ plan.quantity }}</td>
                  <td>
                    <span class="badge" :class="{
                      'badge-success': plan.status === '已完成',
                      'badge-warning': plan.status === '进行中',
                      'badge-danger': plan.status === '挂起'
                    }">{{ plan.status }}</span>
                  </td>
                  <td>
                    <div style="display: flex; align-items: center;">
                      <div
                        style="width: 60px; height: 8px; background-color: #e9ecef; border-radius: 4px; margin-right: 10px;">
                        <div
                          :style="{ width: plan.progress + '%', height: '100%', backgroundColor: plan.progress === 100 ? '#34c759' : '#2c80ff', borderRadius: '4px' }">
                        </div>
                      </div>
                      {{ plan.progress }}%
                    </div>
                  </td>
                  <td>{{ plan.deadline }}</td>
                  <td>
                    <div class="actions">
                      <button class="btn btn-sm">查看</button>
                      <button class="btn btn-sm" v-if="plan.status !== '已完成'">编辑</button>
                    </div>
                  </td>
                </tr>
              </template>
              <template v-else>
                <tr>
                  <td colspan="7" class="empty-data">
                    <div class="empty-placeholder">
                      <font-awesome-icon icon="project-diagram" class="empty-icon" />
                      <p>暂无生产计划数据</p>
                      <button class="btn btn-sm" @click="showPlanModal = true">创建新计划</button>
                    </div>
                  </td>
                </tr>
              </template>
            </tbody>
          </table>
        </div>
      </div>
    </main>
  </div>

  <!-- 创建计划模态框 -->
  <div v-if="showPlanModal" class="modal-overlay" @click.self="showPlanModal = false">
    <div class="modal">
      <div class="modal-header">
        <h3>创建生产计划</h3>
      </div>
      <div class="modal-body">
        <div class="form-row">
          <label for="cardNumber">卡片号</label>
          <input type="text" id="cardNumber" class="form-control" v-model="newPlan.cardNumber" placeholder="请输入卡片号">
        </div>
        <div class="form-row">
          <label for="product">产品名称</label>
          <input type="text" id="product" class="form-control" v-model="newPlan.product" placeholder="请输入产品名称">
        </div>
        <div class="form-row">
          <label for="quantity">数量</label>
          <input type="number" id="quantity" class="form-control" v-model="newPlan.quantity" placeholder="请输入数量">
        </div>
        <div class="form-row">
          <label for="deadline">截止日期</label>
          <input type="date" id="deadline" class="form-control" v-model="newPlan.deadline">
        </div>
      </div>
      <div class="modal-footer">
        <button class="btn" @click="addPlan">提交</button>
        <button class="btn btn-warning" @click="showPlanModal = false">取消</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const plans = reactive([]);
const showPlanModal = ref(false);
const newPlan = reactive({
  cardNumber: '',
  product: '',
  quantity: '',
  deadline: ''
});

// 登出功能
const logout = () => {
  localStorage.removeItem('isLoggedIn');
  router.push('/login');
};

// 加载示例数据
const loadSampleData = () => {
  plans.push(...[
    { id: 1, cardNumber: 'C202308001', product: '轴承套圈', quantity: 1000, status: '进行中', progress: 65, deadline: '2023-08-30' },
    { id: 2, cardNumber: 'C202308002', product: '齿轮组件', quantity: 500, status: '待审核', progress: 0, deadline: '2023-09-05' },
    { id: 3, cardNumber: 'C202308003', product: '液压阀体', quantity: 800, status: '已完成', progress: 100, deadline: '2023-08-25' },
    { id: 4, cardNumber: 'C202308004', product: '传动轴', quantity: 1200, status: '挂起', progress: 30, deadline: '2023-09-10' },
    { id: 5, cardNumber: 'C202308005', product: '外壳铸件', quantity: 600, status: '进行中', progress: 45, deadline: '2023-09-02' }
  ]);
};

// 添加新计划
const addPlan = () => {
  if (newPlan.cardNumber && newPlan.product && newPlan.quantity && newPlan.deadline) {
    plans.unshift({
      id: plans.length + 1,
      cardNumber: newPlan.cardNumber,
      product: newPlan.product,
      quantity: newPlan.quantity,
      status: '待审核',
      progress: 0,
      deadline: newPlan.deadline
    });

    // 重置表单
    newPlan.cardNumber = '';
    newPlan.product = '';
    newPlan.quantity = '';
    newPlan.deadline = '';

    showPlanModal.value = false;
  }
};

// 计算统计信息
const stats = computed(() => {
  const total = plans.length;
  const inProgress = plans.filter(p => p.status === '进行中').length;
  const completed = plans.filter(p => p.status === '已完成').length;
  const pending = plans.filter(p => p.status === '待审核').length;

  return { total, inProgress, completed, pending };
});

// 组件挂载时加载示例数据
onMounted(() => {
  loadSampleData();
});
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Microsoft YaHei', sans-serif;
}

body {
  background-color: #f5f7fa;
  color: #333;
  line-height: 1.6;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 15px;
}

/* 主界面样式 */
.app-container {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 250px;
  background-color: #1a1a1a;
  color: white;
  padding: 1.5rem 0;
}

.sidebar-header {
  padding: 0 1.5rem 1.5rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-menu {
  list-style: none;
  padding: 1rem 0;
}

.sidebar-menu li {
  padding: 0.75rem 1.5rem;
  cursor: pointer;
  transition: background-color 0.3s;
  display: flex;
  align-items: center;
}

.sidebar-menu li:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.sidebar-menu li.active {
  background-color: #2c80ff;
}

.sidebar-menu svg {
  margin-right: 0.75rem;
  width: 20px;
  text-align: center;
}

.main-content {
  flex: 1;
  padding: 2rem;
  overflow-y: auto;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  padding-bottom: 1rem;
  border-bottom: 1px solid #dee2e6;
}

.header h2 {
  color: #1a1a1a;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-info img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 0.75rem;
}

.logout-link {
  cursor: pointer;
  color: #2c80ff;
  font-weight: 500;
}

.logout-link:hover {
  text-decoration: underline;
}

.card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  margin-bottom: 1.5rem;
  overflow: hidden;
}

.card-header {
  padding: 1.25rem;
  border-bottom: 1px solid #dee2e6;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-body {
  padding: 1.25rem;
}

.stats-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1.5rem;
  margin-bottom: 2rem;
}

.stat-card {
  background: white;
  border-radius: 8px;
  padding: 1.5rem;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  text-align: center;
}

.stat-card h3 {
  font-size: 2rem;
  color: #2c80ff;
  margin-bottom: 0.5rem;
}

.stat-card p {
  color: #6c757d;
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  padding: 0.75rem 1rem;
  text-align: left;
  border-bottom: 1px solid #dee2e6;
}

th {
  background-color: #f8f9fa;
  font-weight: 600;
}

tr:hover {
  background-color: #f8f9fa;
}

.badge {
  padding: 0.35rem 0.65rem;
  border-radius: 50px;
  font-size: 0.75rem;
  font-weight: 600;
}

.badge-success {
  background-color: rgba(52, 199, 89, 0.15);
  color: #34c759;
}

.badge-warning {
  background-color: rgba(255, 149, 0, 0.15);
  color: #ff9500;
}

.badge-danger {
  background-color: rgba(255, 59, 48, 0.15);
  color: #ff3b30;
}

.actions {
  display: flex;
  gap: 0.5rem;
}

.btn {
  display: inline-block;
  padding: 0.75rem 1.5rem;
  background-color: #2c80ff;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: 500;
  transition: background-color 0.3s;
  text-align: center;
  width: 100%;
}

.btn:hover {
  background-color: #1a6de0;
}

.btn-warning {
  background-color: #ff9500;
}

.btn-warning:hover {
  background-color: #e57c00;
}

.btn-sm {
  padding: 0.35rem 0.75rem;
  font-size: 0.875rem;
  width: auto;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
}

.modal-header {
  padding: 1.25rem;
  border-bottom: 1px solid #dee2e6;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-body {
  padding: 1.25rem;
}

.form-row {
  margin-bottom: 1rem;
}

.form-row label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
}

.form-row input {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid #dee2e6;
  border-radius: 5px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

.form-row input:focus {
  border-color: #2c80ff;
  outline: none;
}

.modal-footer {
  display: flex;
  gap: 10px;
  padding: 1.25rem;
  border-top: 1px solid #dee2e6;
  justify-content: flex-end;
}

.empty-data {
  text-align: center;
}

.empty-placeholder {
  padding: 3rem 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.empty-icon {
  font-size: 3rem;
  color: #dee2e6;
  margin-bottom: 1rem;
}

.empty-placeholder p {
  color: #6c757d;
  margin-bottom: 1.5rem;
}

.empty-placeholder .btn {
  width: auto;
}

@media (max-width: 768px) {
  .app-container {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    padding: 1rem 0;
  }

  .stats-container {
    grid-template-columns: 1fr;
  }
}
</style>